package com.wyl.selenium.service.impl;

import cn.hutool.core.bean.copier.BeanCopier;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyl.selenium.bean.ProjectInterfacePath;
import com.wyl.selenium.common.CommonPage;
import com.wyl.selenium.entity.ProjectInterface;
import com.wyl.selenium.mapper.ProjectInterfaceMapper;
import com.wyl.selenium.req.ProjectInterfaceReq;
import com.wyl.selenium.service.ProjectBaseInfoService;
import com.wyl.selenium.service.ProjectInterfaceService;
import com.wyl.selenium.vo.ProjectBaseInfoVo;
import com.wyl.selenium.vo.ProjectInterfaceVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author WuYiLong
 * @Date 2023/11/13 11:09
 */
@Slf4j
@Service
public class ProjectInterfaceServiceImpl extends ServiceImpl<ProjectInterfaceMapper, ProjectInterface> implements ProjectInterfaceService {


    @Autowired
    private ProjectBaseInfoService projectBaseInfoService;

    @Override
    public void importProjectInterface(MultipartFile multipartFile,Long projectBaseInfoId) throws IOException {
        InputStream inputStream = multipartFile.getInputStream();
        String json = IoUtil.read(inputStream, Charset.defaultCharset());
        ProjectInterfacePath projectInterfacePath = JSONUtil.toBean(json, ProjectInterfacePath.class);
        Map<String, Map<String, ProjectInterfacePath.Method>> pathMaps = projectInterfacePath.getPaths();
        List<ProjectInterface> projectInterfaces = new ArrayList<>();
        pathMaps.forEach((k,v)->{
            ProjectInterface projectInterface = new ProjectInterface();
            projectInterface.setPath(k);
            projectInterface.setProjectBaseInfoId(projectBaseInfoId);
            v.forEach((k1,v1)->{
                String summary = v1.getSummary();
                List<String> tags = v1.getTags();
                projectInterface.setMethod(k1);
                if(CollUtil.isNotEmpty(tags)) {
                    projectInterface.setTagName(tags.get(0));
                }
                projectInterface.setPathName(summary);

                if("get".equals(k1)) {
                    projectInterface.setContentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE);
                }else if("post".equals(k1)){
                    List<ProjectInterfacePath.Param> parameters = v1.getParameters();
                    if(CollUtil.isNotEmpty(parameters)) {
                        ProjectInterfacePath.Param param = parameters.get(0);
                        if("body".equals(param.getIn())) {
                            projectInterface.setContentType(MediaType.APPLICATION_JSON_VALUE);
                        }else{
                            projectInterface.setContentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE);
                        }
                    }
                }
                projectInterface.setCreateTime(new Date());
                projectInterface.setUpdateTime(new Date());
                projectInterface.setDeleted(0);

            });
            projectInterfaces.add(projectInterface);
        });

        saveBatch(projectInterfaces);
    }

    @Override
    public CommonPage<List<ProjectInterfaceVo>> pageProjectInterface(ProjectInterfaceReq req) {
        Page<ProjectInterface> page = new Page<>(req.getCurrentPage(), req.getPageSize());
        LambdaQueryWrapper<ProjectInterface> wrapper = Wrappers.lambdaQuery(ProjectInterface.class);

        if(StrUtil.isNotBlank(req.getPathName())) {
            wrapper.like(ProjectInterface::getPathName,req.getPathName());
        }
        if(StrUtil.isNotBlank(req.getTagName())) {
            wrapper.like(ProjectInterface::getTagName,req.getTagName());
        }
        if(req.getProjectBaseInfoId() != null) {
            wrapper.eq(ProjectInterface::getProjectBaseInfoId,req.getProjectBaseInfoId());
        }
        Page<ProjectInterface> projectInterfacePage = page(page, wrapper);
        if(projectInterfacePage.getTotal()<=0) {
            return new CommonPage();
        }
        List<ProjectInterface> records = projectInterfacePage.getRecords();
        List<Long> projectBaseInfoIds = records.stream().map(ProjectInterface::getProjectBaseInfoId).collect(Collectors.toList());
        List<ProjectBaseInfoVo> projectBaseInfoVos = projectBaseInfoService.listProjectBaseInfo(projectBaseInfoIds);
        Map<Long, String> collectMap = projectBaseInfoVos.stream().collect(Collectors.toMap(ProjectBaseInfoVo::getId, ProjectBaseInfoVo::getName));


        List<ProjectInterfaceVo> projectInterfaceVos = projectInterfacePage.getRecords().stream().map(m -> {
            ProjectInterfaceVo projectInterfaceVo = new ProjectInterfaceVo();
            BeanCopier<ProjectInterfaceVo> projectInterfaceVoBeanCopier = BeanCopier.create(m, projectInterfaceVo, new CopyOptions());
            ProjectInterfaceVo copy = projectInterfaceVoBeanCopier.copy();
            copy.setProjectBaseInfoName(collectMap.get(m.getProjectBaseInfoId()));
            return projectInterfaceVo;
        }).collect(Collectors.toList());

        CommonPage<List<ProjectInterfaceVo>> commonPage = new CommonPage(req.getCurrentPage(), req.getPageSize());
        commonPage.setList(projectInterfaceVos);
        commonPage.setTotal(projectInterfacePage.getTotal());
        return commonPage;
    }
}
